// import { Net } from "@/net/Net";
import H5Websocket from "@/net/Net1";
// import { NetPacket } from "@/netBase/NetPacket";
// import { Local, needLoginApi } from "@/utils/storage";
// import { storeToRefs } from 'pinia';
// import pinia from '@/store/index';
// import { User } from '@/store/user';


let lastTouchEnd = 0; //更新手指弹起的时间
document.documentElement.addEventListener("touchstart", function (event) {
  //多根手指同时按下屏幕，禁止默认行为
  if (event.touches.length > 1) {
    event.preventDefault();
  }
}, false);

document.documentElement.addEventListener(
  "touchend",
  function (event) {
    let now = new Date().getTime();
    if (now - lastTouchEnd <= 300) {
      //当两次手指弹起的时间小于300毫秒，认为双击屏幕行为
      event.preventDefault();
    } else {
      // 否则重新手指弹起的时间
      lastTouchEnd = now;
    }
  },
  false
);
//阻止双指放大页面
document.documentElement.addEventListener("gesturestart", function (event) {
  event.preventDefault();
});
document.oncontextmenu = function (e) {
  e.preventDefault()
}
let deferredPrompt: any;
window.addEventListener('beforeinstallprompt', (e) => {
  e.preventDefault();
  deferredPrompt = e;
  // 显示安装按钮
});
// 在用户点击安装按钮时调用
window.addEventListener('click', () => {
  const appinstall_popup = sessionStorage.getItem('appinstall_popup')
  if (appinstall_popup || !deferredPrompt) return
  deferredPrompt.prompt();
  deferredPrompt.userChoice.then((_choiceResult: any) => {
    // if (choiceResult.outcome === 'accepted') {
    //   console.log('User accepted the install prompt');
    // } else {
    //   console.log('User dismissed the install prompt');
    // }
    sessionStorage.setItem('appinstall_popup', '1')
    deferredPrompt = null;
  });
});

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/service-worker.js')
    .then((registration) => {
      console.log('Service Worker registered with scope:', registration.scope);
    })
    .catch((error) => {
      console.log('Service Worker registration failed:', error);
    });
}
// window.addEventListener('offline',function(){
//   alert('Please check if your network connection is normal')
// },true)

// const waitForCondition = (next: Function) => {
//   return new Promise(async () => {
//     const { wsOpen } = storeToRefs(User(pinia));
//     let timer
//     if (wsOpen.value) {
//       next()
//       clearTimeout(timer)
//       timer = null
//     } else {
//       timer = setTimeout(() => {
//         console.log(99999);

//         waitForCondition(next);
//       }, 400);
//     }
//   })
// }
// const next = () => {
//   // debugger
//   if (Local.get('user') && Local.get('user').token) {
//     needLoginApi(true)
//   } else {
// let req_check_version_req = NetPacket.req_check_version();
// req_check_version_req.version = 1;
// // Net.instance.sendRequest(req_check_version_req)

// H5Websocket.sendMessageHandler(req_check_version_req);

// Net.instance.reConnect();
// let req = NetPacket.req_init_connect();
// // Net.instance.sendRequest(req);
// H5Websocket.sendMessageHandler(req);
//   }
// }
document.addEventListener("visibilitychange", async () => {
  if (document.visibilityState == "visible") {
    if (H5Websocket.instance == null) {
      window.location.reload();
    }
    // await H5Websocket.conectWebsocket()
    // next()
    // await waitForCondition(next)

    console.log("进入前台");
  }
  if (document.visibilityState == "hidden") {

    // H5Websocket.instance?.close()
    // Net.instance.closeSocket()
    console.log("切换到后台")
  }
})


